var common = require("../../utils/common.js");
//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    data: [],
    banner: '',
    tip: [],
    longitude: 118.6648443788737,
    latitude: 31.90991590886252,
    getUserInfoFail: false,
    type: '', //分享进来的各种数据
    id: '', //如果是顾问详情页分享进来的，则有
    loading: true,
    noVR: false, //无vr地址
    hasWaterLogo: false, //是否有水印
    isVrshow: false, //是否有vr看房
    xcxopenid: '', //获取小程序openid
    isGetPhone: '', //是否需要获取手机号授权，默认为true
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function (options) {
    let isIphoneX = app.globalData.isIphoneX;
    this.setData({
      isIphoneX: isIphoneX
    });
    if (app.globalData.xcxtype.hasLogo == '1') {
      //显示水印
      this.setData({
        hasWaterLogo: true
      })
    } else {
      this.setData({
        hasWaterLogo: false
      })
    };
    if (app.globalData.xcxtype.hasVr == "0") {
      this.setData({
        isVrshow: false
      })
    } else {
      this.setData({
        isVrshow: true
      })
    };
    wx.hideTabBar();
    getApp().editTabBar();
  },
  load: function () {
    var that = this;
    common.ajaxPost(
      'portal/wlpBuilding/queryBuilding.action', {
        // appId :'wx067ac066b8ee77fd'
        id: app.globalData.bid
      },
      function (res) {
        if (res && res.length > 0) {
          console.log(res);
          var data = res[0];
          var banner = data.banners.split(";")[0];
          var coverPic = data.coverPic;
          var tips = data.houseLable.split("|");
          var position = common.bd09togcj02(data.longitude, data.latitude);
          var vrUrl = data.vrUrl;
          var propertyName = data.name;
          // console.log(vrUrl)
          if (vrUrl == '') {
            that.setData({
              noVR: false,
              vrUrl: vrUrl
            })
          } else {
            that.setData({
              noVR: true,
              vrUrl: vrUrl
            })
          }

          that.setData({
            data: data,
            banner: banner,
            coverPic: coverPic,
            tips: tips,
            longitude: position[0],
            latitude: position[1],

          })
          wx.setNavigationBarTitle({
            title: propertyName
          });
          wx.hideLoading()
          // that.toPage()
        }
      }
    )
  },
  onShow: function () {
    this.setData({
      loading: app.globalData.loading
    });
    //获取openid
    var that = this;
    that.queryWlpAuto();
    that.modifyOnlookers();
    // this.load();
  },



  //上面的是 根据openId  查询是否已经授权过手机号码
  queryWlpAuto: function () {
    var that = this;
    common.ajaxPost(
      'portal/wlpAuto/queryWlpAuto.action', {
        openId: app.globalData.xcxOpenId
      },
      function (res) {
        if (res && res["result"]) {
          if (res.list == '' || res.list == []) {
            //数据为空，显示
            that.setData({
              isGetPhone: true
            });
            that.load();
          } else {
            console.log(res);
            wx.setStorage({
              key: 'phone',
              data: res.list[0].phone,
            })
            that.load();
          }
        }
      }
    )
  },

  modifyOnlookers: function () {
    //添加围观人员
    var nikeName;
    var headImg;
    if(app.globalData.alluserInfo){
      nikeName = app.globalData.alluserInfo.nickName;
      headImg = app.globalData.alluserInfo.avatarUrl;
    }else{
      nikeName = app.globalData.userInfoweixin.nickName;
      headImg = app.globalData.userInfoweixin.avatarUrl;
    }
    common.ajaxPost(
      'portal/wlpOnlookers/modifyOnlookers.action', {
        nikeName: nikeName,
        headImg: headImg,
        bid: app.globalData.bid,
        openId: app.globalData.xcxOpenId
      },
      function (res) {
        if (res && res["result"]) {
          console.log(res);
        }
      }
    )
  },




gotoOrder: function () {
  if (this.data.isGetPhone === true) {

  } else {
    wx.navigateTo({
      url: '/pages/order/order',
    })
  }
},
getPhoneNumber: function (e) {
  //第二个是授权手机号码，进行解析
  var that = this;
  var xcxid;
  if (that.data.xcxid) {
    xcxid = that.data.xcxid
  } else if (app.globalData.xcxshareid) {
    xcxid = app.globalData.xcxshareid
  } else {
    xcxid = app.globalData.queryWlpXcxByParams[0].id
  }
  wx.login({
    success: res => {
      if (res.code) {
        var code = res.code;
        common.ajaxPost(
          'portal/wlpAuto/autoPhone.action', {
            xcxId: xcxid,
            iv: e.detail.iv,
            encryptedData: e.detail.encryptedData,
            code: code
          },
          function (res) {
            if (res && res["result"]) {
              //存手机号码
              wx.setStorage({
                key: 'phone',
                data: res.phoneNumber,
              });
              wx.navigateTo({
                url: '/pages/order/order',
              })
            }
          }
        )
      } else {
        console.log('登录失败！' + res.errMsg);
      }
    }
  });
},
todetail: function () {
  wx.switchTab({
    url: '/pages/detail/detail'
  })
},
calling: function (e) {
  wx.makePhoneCall({
    phoneNumber: e.currentTarget.dataset.tel, //此号码并非真实电话号码，仅用于测试
    success: function () {
      console.log("拨打电话成功！")
    },
    fail: function () {
      console.log("拨打电话失败！")
    }
  })
},
glos: function (event) {
  var that = this;
  console.log(that.data);
  common.openPosition(that.data.latitude, that.data.longitude, that.data.data.address, that.data.data.addressDesc)
},
onShareAppMessage: function (res) {
  console.log(res);
  console.log(app);
  var name = this.data.data.name;
  var that = this;
  var xcxids;
  if (app.globalData.xcxshareid) {
    xcxids = app.globalData.xcxshareid
  } else {
    xcxids = app.globalData.queryWlpXcxByParams[0].id
  }
  console.log(xcxids);
  console.log(name);
  console.log(app.globalData.lpid)
  //分享楼盘id
  return {
    title: name,
    path: '/pages/index/index?xcxid=' + xcxids + "&lpid=" + app.globalData.lpid,
    success: function (res) {
      console.log(res);
      // 转发成功
      //console.log('/pages/index/index?xcxid=' + xcxids + "&lpid=" + app.globalData.lpid);
    },
    fail: function (res) {
      // 转发失败
      console.log(res);
    }
  }
},
gotoList: function () {

  wx.redirectTo({
    url: '../roomDetail/roomDetail',
  })
},
changeTab: function (e) {
  var url = e.currentTarget.dataset.url;
  wx.switchTab({
    url: url
  })
},
})